/**
 * 
 */
package edu.caltech.android.waveforms;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import edu.caltech.android.R;
import edu.caltech.android.ShakeAlertActivity;
import edu.caltech.android.ShakeAlertService;
import edu.caltech.android.c2dm.HelpActivity;
import edu.caltech.android.picking.Pick;
import edu.caltech.android.sensor.accel.AccelSample;
import edu.caltech.android.sensor.accel.Accelerometer;

/**
 * Activity to display the accelerometer waveforms.
 * 
 *  
 * @author mfaulk
 * 
 */
public class WaveformDisplay extends Activity {
	private static final String TAG = "WaveformDisplay";
	private static final int SHAKE_MAP_ID = Menu.FIRST;
	private static final int INFO_ID = Menu.FIRST + 1;
	private static final int QUIT_ID = Menu.FIRST + 2;

	private AccelerationBroadcastReceiver accelReceiver;
	private WaveformView2 waveformView;
	private PickBroadcastReceiver pickBroadcastReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// if this can't be resolved, make sure android.R is not being imported
		setContentView(R.layout.waveform);

		waveformView = (WaveformView2)findViewById(R.id.waveformView);

		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean retVal = super.onCreateOptionsMenu(menu);

		menu.add(0, SHAKE_MAP_ID, 0, "Shake Map");
		menu.add(0, INFO_ID, 0, "About");
		menu.add(0, QUIT_ID, 0, "Quit");
		return retVal;
	}

	@Override
	protected void onResume(){
		super.onResume();
		
		// register the acceleration broadcast receiver (this could be done in
		// XML as an intent-filter)
		IntentFilter filter = new IntentFilter(Accelerometer.UI_ACCEL_SAMPLES);
		accelReceiver = new AccelerationBroadcastReceiver();
		registerReceiver(accelReceiver, filter);
		
		// ---- register for pick broadcasts ---- //
		// register the acceleration broadcast receiver (this could be done in
		// XML as an intent-filter)
		IntentFilter pickFilter = new IntentFilter(ShakeAlertService.PICK);
		pickBroadcastReceiver = new PickBroadcastReceiver();
		registerReceiver(pickBroadcastReceiver, pickFilter);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(accelReceiver);
		unregisterReceiver(pickBroadcastReceiver);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case INFO_ID:
			Log.d(TAG,"launching intent for system info");
//			Intent infoIntent = new Intent(WaveformDisplay.this,
//					ClientInfo.class);
//			startActivity(infoIntent);
			//finish();
			startActivity(new Intent(this, HelpActivity.class));
			break;
		case SHAKE_MAP_ID:
			Log.d(TAG,"launching intent for map");
			Intent alertIntent = new Intent(WaveformDisplay.this,
					ShakeAlertActivity.class);
			startActivity(alertIntent);
			finish();
			break;
		case QUIT_ID:
			System.out.println("quit...");
			stopService(new Intent(WaveformDisplay.this, ShakeAlertService.class));
			finish();
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	/**
	 * receive accelerometer UI update broadcasts
	 */
	private class AccelerationBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO: this name should be a static final string
			Bundle bundle = intent.getBundleExtra("accelSamples");
			if (bundle != null) {
				ArrayList<AccelSample> accelSamples = bundle.getParcelableArrayList(Accelerometer.UI_ACCEL_BUNDLE);
				waveformView.addAccelSamples(accelSamples);
			}
		}
	}
	
	
	/**
	 * receive Pick broadcasts
	 */
	private class PickBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {

			if (intent.getAction().equals(ShakeAlertService.PICK)) {

				Pick p = intent
						.getParcelableExtra(ShakeAlertService.PICK_KEY);
				if (p != null && hasWindowFocus()) {
					Log.d("WaveformDisplay", "got pick broadcast");
					Toast toast = Toast.makeText(getApplicationContext(), "Pick",
							Toast.LENGTH_SHORT);
					toast.show();
				}

			}
		}
	} // end of class

}
